#include "MainWindow.h"
#include "ui_MainWindow.h"



#include <Defines.h>
#include <CampaignItem.h>
#include <CharacterItem.h>
#include <ItemFactory.h>



#include <XmlSerializer.h>
#include <FileHandler.h>



#include <QMessageBox>
#include <QFileDialog>
#include <QTreeView>
#include <QTimer>



MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    FileHandler::installSerializer(new XmlSerializer());

    ui->setupUi(this);
}



MainWindow::~MainWindow()
{
    delete ui;
}



void MainWindow::closeEvent(QCloseEvent *e)
{
    on_actionQuit_triggered();

    QMainWindow::closeEvent(e);
}



void MainWindow::resizeEvent(QResizeEvent *e)
{
    QMainWindow::resizeEvent(e);

    ui->tabWidget->onRefresh();
}



void MainWindow::on_actionSave_triggered()
{
    ui->tabWidget->onSave();
}



void MainWindow::on_actionSave_As_triggered()
{
    ui->tabWidget->onSaveAs();
}



void MainWindow::on_actionOpen_triggered()
{
    ItemRoot* root = ui->tabWidget->onOpen();
    if (!root) return;

    CharacterItem* character = dynamic_cast<CharacterItem*>(root);
    if (!character)
    {
        ui->tabWidget->onNew(root);
        return;
    }

    qDebug(character->campaignFile().toAscii());

    CampaignItem* campaign = dynamic_cast<CampaignItem*>(FileHandler::open(character->campaignFile()));

    if (!campaign)
    {

        campaign = dynamic_cast<CampaignItem*>(ui->tabWidget->onOpen());
    }

    if (!campaign)
    {
        character->deleteLater();
        return;
    }

    character->setCampaign(campaign);


    ui->tabWidget->onNew(character);
}



void MainWindow::on_tabWidget_tabCloseRequested(int index)
{
    ui->tabWidget->onClose(index);
    ui->tabWidget->onRefresh();
}



void MainWindow::on_actionClose_Current_triggered()
{
    int index = ui->tabWidget->currentIndex();
    on_tabWidget_tabCloseRequested(index);
}



void MainWindow::on_actionQuit_triggered()
{
    int tabs = ui->tabWidget->count();
    for (int index = 0; index < tabs; index++)
    {
        on_tabWidget_tabCloseRequested(index);
    }

    close();
}



void MainWindow::on_actionRename_triggered()
{
    ui->tabWidget->onBeginRename(ui->tabWidget->currentIndex());
}



void MainWindow::on_actionRefresh_triggered()
{
    ui->tabWidget->onRefresh();
}




void MainWindow::on_actionDescription_toggled(bool )
{
}



void MainWindow::on_actionAdd_Group_triggered()
{
    ui->tabWidget->onAddGroup();
}



void MainWindow::on_actionAdd_Item_triggered()
{
    ui->tabWidget->onAddItem();
}



void MainWindow::on_actionAdd_Reference_triggered()
{
    ui->tabWidget->onAddReference();
    return;
}



void MainWindow::on_actionBegin_Edit_triggered()
{
    QTreeView* view = ui->tabWidget->tabView();

    if (!view) return;

    QModelIndex index = view->currentIndex();
    if (!index.isValid()) return;

    view->edit(index);
}



void MainWindow::on_actionCampaign_triggered()
{
    ItemRoot* root = dynamic_cast<ItemRoot*>(ItemFactory::create("CampaignItem"));
    root->setData(AbstractItem::NameData, tr("New Campaign"));

    ui->tabWidget->onNew(root);
}

void MainWindow::on_actionCharacter_triggered()
{
    CharacterItem* character = dynamic_cast<CharacterItem*>(ItemFactory::create("CharacterItem"));
    if (!character) return;

    character->setData(AbstractItem::NameData, tr("New Character"));

    CampaignItem* campaign = dynamic_cast<CampaignItem*>(ui->tabWidget->onOpen());
    if (!campaign)
    {
        character->deleteLater();
        return;
    }

    character->setCampaign(campaign);
    ui->tabWidget->onNew(character);
}
